route.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // app/api/branches/[branch]/[year]/[month]/days/route.js
  2. import { NextResponse } from "next/server";
  3. import { listDays } from "@/lib/storage";
  4. import { getSession } from "@/lib/auth/session";
  5. import { canAccessBranch } from "@/lib/auth/permissions";
  6. /**
  7. * GET /api/branches/[branch]/[year]/[month]/days
  8. */
  9. export async function GET(request, ctx) {
  10. const session = await getSession();
  11. if (!session) {
  12. return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  13. }
  14. const { branch, year, month } = await ctx.params;
  15. console.log("[/api/branches/[branch]/[year]/[month]/days] params:", {
  16. branch,
  17. year,
  18. month,
  19. });
  20. if (!branch || !year || !month) {
  21. return NextResponse.json(
  22. { error: "branch, year oder month fehlt" },
  23. { status: 400 }
  24. );
  25. }
  26. if (!canAccessBranch(session, branch)) {
  27. return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  28. }
  29. try {
  30. const days = await listDays(branch, year, month);
  31. return NextResponse.json({ branch, year, month, days });
  32. } catch (error) {
  33. console.error("[/api/branches/[branch]/[year]/[month]/days] Error:", error);
  34. return NextResponse.json(
  35. { error: "Fehler beim Lesen der Tage: " + error.message },
  36. { status: 500 }
  37. );
  38. }
  39. }